package quirk;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;

public class Util {
  
  /**
   * Gets the first line from the stream after skipping the specified amount of lines
   * @param bis
   * @param linesToSkip
   * @return
   */
  public static String getLineFromStream(BufferedReader bis, int linesToSkip) {
    String line = "";    
    try {
      for(int i = 0; i < linesToSkip; i++)
        bis.readLine();
      line = bis.readLine();
    } catch (Exception e) {
      System.err.println("There was an error reading the reader.");
    }
    return line;
  }
  
  /**
   * Checks the next char in an input stream
   * @param    reader    The input stream to read
   * @return    Next character in input stream
   * @throws    IOException
   */
  public static int peek(Reader reader) throws IOException {
    int nextChar = 0;
    if(reader.markSupported()) {
      reader.mark(32);      
      nextChar = reader.read();
      reader.reset();
    }
    return nextChar;
  }
  
  /**
   * Resizes a char array when necessary
   * @param    charArray    Char array to resize
   * @param    newSize    New size of array
   */
  public static char[] resizeCharArray(char[] charArray, int newSize) {
    char[] newCharArray = new char[newSize];
    for (int i = 0; i < charArray.length; i++) {
      newCharArray[i] = charArray[i];
    }
    return newCharArray;
  }

  /**
   * Prints a usage error to STDERR
   * @param error    Error to print
   */
  public static void usageError(String error) {
    System.err.println(error);
    System.exit(-1);
  }
}
